package com.smarthome.web.property.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.smarthome.core.operationservicemanager.ExpressFeeService;
import com.smarthome.domain.bean.ShExpressFee;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 
* @ClassName: ExpressFeeController
* @Description: 快递费controller
* @author cyj
* @date 2016年4月20日 上午10:41:39
*
 */
@Controller
@RequestMapping("/expressFeeController")
public class ExpressFeeController {
	
	@Resource
    private ExpressFeeService expressFeeService;
	
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShExpressFee expressFee,PageHelper page){
		return expressFeeService.qeuryDataGrid(expressFee, page);
	}
	
	
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request,Long expressId) {
		request.setAttribute("expressId", expressId);
		request.setAttribute("provinceTree", JSONObject.toJSON(expressFeeService.getExpressFeeProvince(expressId)));
        return "/system/operationServiceManager/expressFeeAdd";
	}
	
	
	
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShExpressFee expressFee,String provinceIds) {
		if(StringUtils.isBlank(provinceIds)) {
			return Result.failure("请选择至少一个区域！");
		}
		if(expressFee.getFirstWeight() == null) {
			return Result.failure("请输入首重！");
		}
		if(expressFee.getFirstWeightFee() == null) {
			return Result.failure("请输入首重费用！");
		}
		if(expressFee.getAddedWeightFee() == null) {
			return Result.failure("请输入续重费用！");
		}
		if(expressFee.getAging() == null) {
			return Result.failure("请输入时效！");
		}
		if(expressFee.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		Date nowDate = new Date();
		expressFee.setGmtCreate(nowDate);
		expressFee.setGmtModify(nowDate);
		try{
			String[] provinceIdsArray = provinceIds.split(",");
			ShExpressFee fee = new ShExpressFee();
			fee.setExpressId(expressFee.getExpressId());
			fee.addQueryData("provinceIds", provinceIdsArray);
			
			List<ShExpressFee> hads = expressFeeService.getList(fee);
			if(hads != null && !hads.isEmpty()) {
				int length = hads.size();
				StringBuilder provinceNames = new StringBuilder();
				for(int i = 0;i < length;i++) {
					if(i != 0) {
						provinceNames.append("、");
					}
					provinceNames.append(hads.get(i).getProvinceName());
				}
				return Result.failure(provinceNames.toString() + "已有费用设置，请从新选择！");
			}
			
			List<ShExpressFee> expressFeeList = new ArrayList<>();
			for(int i = 0;i < provinceIdsArray.length;i++) {
				if(!"0".equals(provinceIdsArray[i])) {
					fee = new ShExpressFee();
					BeanUtils.copyProperties(expressFee, fee);
					fee.setProvinceId(Long.parseLong(provinceIdsArray[i]));
					expressFeeList.add(fee);
				}
			}
			
			expressFeeService.add(expressFeeList.toArray(new ShExpressFee[expressFeeList.size()]));
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
		}
		return Result.success("新增成功！");
	 }
	
	
	
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShExpressFee expressFee) {
		if(expressFee.getFirstWeight() == null) {
			return Result.failure("请输入首重！");
		}
		if(expressFee.getFirstWeightFee() == null) {
			return Result.failure("请输入首重费用！");
		}
		if(expressFee.getAddedWeightFee() == null) {
			return Result.failure("请输入续重费用！");
		}
		if(expressFee.getAging() == null) {
			return Result.failure("请输入时效！");
		}
		if(expressFee.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		try{
			
			
			expressFee.setGmtModify(new Date());
			
			
			expressFeeService.saveOrUpdate(expressFee);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("修改成功！");
	 }
		
	  
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,Long id){
		request.setAttribute("expressFee", expressFeeService.getById(id));
		return "/system/operationServiceManager/expressFeeEdit";
	}
	
	
	  
	  
	 
	  @RequestMapping("/delete")
	  @ResponseBody
	  public Result delete(Long id) {
		  try{
			  expressFeeService.delete(id);
		  } catch(Exception e){
			  return Result.failure("快递费被引用，不能删除");
		  }
		  return Result.success("删除成功！");
	  }
	  
	  
}
